from PyQt4 import QtGui,QtCore
from ftllibs.FTL import CHAMPS as CHAMPS
import copy

class UI_ChampDisplay(QtGui.QWidget):
    ChampLevelChange = QtCore.pyqtSignal([str])
    def __init__(self,parent=None,ChampID=None,ChampLevel=18):
        super(UI_ChampDisplay,self).__init__()
        self.Champ=parent.Champ
        self.ChampID=ChampID
        self.ChampDefaultPixmap=QtGui.QPixmap('images/ui/NoChamp.jpg')
#        print "Defaultpix: ",self.ChampDefaultPixmap
        self.ChampPixmap = QtGui.QPixmap(128,128)
	self.ChampPixmap.fill()
#        print "Champpixmap: ",self.ChampPixmap
        if not self.ChampID == None:
            self.SetChamp(self.ChampID)
        else:
            self.ChampPixmap = self.ChampDefaultPixmap
#        print "Defaultpix: ",self.ChampDefaultPixmap
#        print "Champpixmap: ",self.ChampPixmap
        self.ChampLevel=ChampLevel
        self.ChampLevelComboBox=QtGui.QComboBox(self)
        self.ChampLevelComboBox.addItem('1')
        self.ChampLevelComboBox.addItem('6')
        self.ChampLevelComboBox.addItem('12')
        self.ChampLevelComboBox.addItem('16')
        self.ChampLevelComboBox.addItem('18')
        self.ChampLevelComboBox.insertSeparator(6)
        self.ChampLevelComboBox.addItem('1')
        self.ChampLevelComboBox.addItem('2')
        self.ChampLevelComboBox.addItem('3')
        self.ChampLevelComboBox.addItem('4')
        self.ChampLevelComboBox.addItem('5')
        self.ChampLevelComboBox.addItem('6')
        self.ChampLevelComboBox.addItem('7')
        self.ChampLevelComboBox.addItem('8')
        self.ChampLevelComboBox.addItem('9')
        self.ChampLevelComboBox.addItem('10')
        self.ChampLevelComboBox.addItem('11')
        self.ChampLevelComboBox.addItem('12')
        self.ChampLevelComboBox.addItem('13')
        self.ChampLevelComboBox.addItem('14')
        self.ChampLevelComboBox.addItem('15')
        self.ChampLevelComboBox.addItem('16')
        self.ChampLevelComboBox.addItem('17')
        self.ChampLevelComboBox.addItem('18')
        self.setFixedSize(120,140)
        self.ChampLevelComboBox.move(71,91)
        self.ChampLevelComboBox.setCurrentIndex(4)
        self.ChampLevelComboBox.currentIndexChanged[str].connect(self.SetChampLevel)

    def SetChampLevel(self,lvl):
        self.ChampLevelChange.emit(lvl)
        
    def SetChamp(self,ChampID):
        self.ChampID=ChampID
        self.SetChampPixmap(self.ChampID)

    def SetChampPixmap(self,ChampID):
        self.ChampPixmap=QtGui.QPixmap(128,128)
        self.pix = CHAMPS.CHAMPS[str(ChampID)].IconData
        self.ChampPixmap.loadFromData(self.pix)
        self.update()

    def paintEvent(self,e):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(0,0,self.ChampPixmap)
        painter.drawText(QtCore.QRectF(0,128,128,12),"%s - %s/%s/%s" % (self.Champ.CreepScore,self.Champ.Kills,self.Champ.Deaths,self.Champ.Assists),QtGui.QTextOption(QtCore.Qt.AlignCenter))

    def mouseDoubleClickEvent(self,e):
        dlg = ScoreDialog(self)
        dlg.exec_()
        ok,cs,kills,deaths,assists = dlg.getReturn()
        if ok:
            self.Champ.CreepScore = int(cs)
            self.Champ.Kills = int(kills)
            self.Champ.Deaths = int(deaths)
            self.Champ.Assists = int(assists)
            self.update()


class ScoreDialog(QtGui.QDialog):
    def __init__(self,parent=None):
        super(ScoreDialog,self).__init__()
        self.parent=parent
        self.Champ=parent.Champ
        self.ok=False
        self.MasterLayout=QtGui.QGridLayout()

        self.CreepScoreLayout=QtGui.QHBoxLayout()
        self.CreepScoreLabel=QtGui.QLabel('CreepScore:')
        self.CreepScoreLineEdit=QtGui.QLineEdit(str(self.Champ.CreepScore))
        self.CreepScoreLayout.addWidget(self.CreepScoreLabel)
        self.CreepScoreLayout.addWidget(self.CreepScoreLineEdit)

        self.KillsLayout=QtGui.QHBoxLayout()
        self.KillsLabel=QtGui.QLabel('Kills:')
        self.KillsLineEdit=QtGui.QLineEdit(str(self.Champ.Kills))
        self.KillsLayout.addWidget(self.KillsLabel)
        self.KillsLayout.addWidget(self.KillsLineEdit)

        self.DeathsLayout=QtGui.QHBoxLayout()
        self.DeathsLabel=QtGui.QLabel('Deaths:')
        self.DeathsLineEdit=QtGui.QLineEdit(str(self.Champ.Deaths))
        self.DeathsLayout.addWidget(self.DeathsLabel)
        self.DeathsLayout.addWidget(self.DeathsLineEdit)

        self.AssistsLayout=QtGui.QHBoxLayout()
        self.AssistsLabel=QtGui.QLabel('Assists:')
        self.AssistsLineEdit=QtGui.QLineEdit(str(self.Champ.Assists))
        self.AssistsLayout.addWidget(self.AssistsLabel)
        self.AssistsLayout.addWidget(self.AssistsLineEdit)

        self.OKButton=QtGui.QPushButton('OK')
        self.CancelButton=QtGui.QPushButton('Cancel')
        self.OKButton.clicked.connect(self.OKClicked)
        self.CancelButton.clicked.connect(self.CancelClicked)
        self.MasterLayout.addLayout(self.CreepScoreLayout,0,0)
        self.MasterLayout.addLayout(self.KillsLayout,1,0,1,2)
        self.MasterLayout.addLayout(self.DeathsLayout,2,0,1,2)
        self.MasterLayout.addLayout(self.AssistsLayout,3,0,1,2)
        self.MasterLayout.addWidget(self.OKButton,4,0,1,1)
        self.MasterLayout.addWidget(self.CancelButton,4,1,1,1)
        self.setLayout(self.MasterLayout)

    def OKClicked(self):
        self.ok = True
        self.close()

    def CancelClicked(self):
        self.ok= False
        self.close()

    def getReturn(self):
        return self.ok,int(self.CreepScoreLineEdit.text()),int(self.KillsLineEdit.text()),int(self.DeathsLineEdit.text()),int(self.AssistsLineEdit.text())
